package com.fdhwws.web.service.impl;

import com.fdhwws.web.dao.UserFootprintsDao;
import com.fdhwws.web.dao.impl.UserFootprintsDaoImpl;
import com.fdhwws.web.domain.UserFootprints;
import com.fdhwws.web.entity.AjaxResult;
import com.fdhwws.web.service.UserFootprintsService;
import com.mysql.cj.util.StringUtils;

import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;

public class UserFootprintsServiceImpl implements UserFootprintsService {

    private final UserFootprintsDao userFootprintsDao = new UserFootprintsDaoImpl();

    @Override
    public AjaxResult findAllUserFootprintsByUid_orderByTime(Integer uid) {
        if (uid == null) {
            return AjaxResult.error("用户ID不能为空", null);
        }
        List<UserFootprints> userFootprints = userFootprintsDao.selectAllByUid_orderByTime(uid);
        return AjaxResult.success("查询成功", userFootprints);
    }

    @Override
    public AjaxResult insertUserFootprints(UserFootprints userFootprints) {
        if (userFootprints.getRoomId() == null || userFootprints.getUid() == null) {
            return AjaxResult.error("房屋ID或用户ID不能为空", null);
        } else {
            userFootprints.setTime(new Timestamp(System.currentTimeMillis()));
            int insert = userFootprintsDao.insert(userFootprints);
            if (insert > 0) {
                return AjaxResult.success("足迹添加成功", null);
            } else {
                return AjaxResult.error("足迹添加失败", null);
            }
        }
    }

    @Override
    public AjaxResult findUserFootprintsByUidAndRoomId(Integer uid, Integer roomId) {
        if (uid == null || roomId == null) {
            return AjaxResult.error("用户ID或房屋ID不能为空", null);
        }
        UserFootprints userFootprints = userFootprintsDao.selectByUidAndRoomId(uid, roomId);
        if (userFootprints == null) {
            return AjaxResult.error("未找到足迹信息", null);
        } else {
            return AjaxResult.success("查询成功", userFootprints);
        }
    }

    @Override
    public AjaxResult updateUserFootprintsById(UserFootprints userFootprints) {
        if (userFootprints.getId() == null) {
            return AjaxResult.error("足迹ID不能为空", null);
        }
        int update = userFootprintsDao.update(userFootprints);
        if (update > 0) {
            return AjaxResult.success("足迹修改成功", null);
        } else {
            return AjaxResult.error("足迹修改失败", null);
        }
    }
}
